package org.dtrd.modules.evaluation.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.PersonalityInfo;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdPersonalityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * @Author QR
 * @Date 2021/11/1 2:18 PM
 */
@Api(tags = "量表评估")
@RestController
public class PersonalityController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdPersonalityService personalityService;

    @ApiOperation("添加卡尔特16种人格因素评估")
    @PostMapping(value = {"/105/47/1054701", "/100/17/1001712"})
    public Result<?> getPersonalityInfo(@RequestBody PersonalityInfo personalityInfo){
        boolean inValidInput = personalityInfo.getInput().size() != Evaluation.ProblemNum.PERSONALITY.getNum();
        if (inValidInput){
            return error(SystemState.ResponseState.STATE_PARAMS_ERROR);
        }
        Optional<Integer> any = personalityInfo.getInput().stream()
                .filter(answer -> answer == -1)
                .findAny();
        if (any.isPresent()) {
            return error(STATE_PARAMS_MISSING);
        }
        PersonalityInfo info = personalityService.savePersonalityInfo(personalityInfo);
        return Result.OK(info);
    }

    @ApiOperation("查询卡尔特16种人格因素评估")
    @GetMapping("/105/47/1054702")
    public Result<?> getPersonalityInfoLatest(@RequestParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        PersonalityInfo personalityInfo = personalityService.getPersonalityInfoLatest(patientId);
        return Result.OK(personalityInfo);
    }
}
